package Helpers;

import Agents.Corpse;
import Agents.Human;
import Agents.Zombie;
import sim.field.grid.SparseGrid2D;
import sim.portrayal.simple.RectanglePortrayal2D;
import sim.util.Bag;
import sim.util.Int2D;

public class Common {
	public static int distance(Int2D a, Int2D b){
		return Math.max(Math.abs(a.x - b.x),Math.abs(a.y - b.y));
	}
	
	public static void removeRectangles(SparseGrid2D yard, int x, int y) {
		Bag b = yard.getObjectsAtLocation(x, y);
		if (b != null) {
			for (Object o : b) {
				if (o instanceof RectanglePortrayal2D) {
					//System.out.println("JE SUPPRIME LE RECTANGLE EN : " + x + " " + y);
					yard.remove(o);
				}
			}
		}
	}
	
	/**
	 * Retourne le plus faible des zombies d'un bag
	 * Ou le plus judicieux à combattre
	 * @param elements
	 * @return Zombie | null
	 */
	public static Zombie getWeakestZombie(Bag elements) {
		Zombie weakest = null;
		for(Object element : elements) {
			if(element instanceof Zombie) {
				Zombie zombie = (Zombie)element;
				if(weakest == null || zombie.getLife() < weakest.getLife()) {
					weakest = zombie;
				}
			}
		}
		return weakest;
	}
	
	public static Human getWeakestHuman(Bag elements) {
		Human weakest = null;
		for(Object element : elements) {
			if(element instanceof Human) {
				Human human = (Human)element;
				if(weakest == null || human.getLife() < weakest.getLife()) {
					weakest = human;
				}
			}
		}
		return weakest;
	}
	
	public static Corpse getCorpse(Bag elements) {
		for(Object element : elements) {
			if(element instanceof Corpse) {
				return (Corpse)element;
			}
		}
		return null;
	}
	
	public static int getRandom(int min, int max) {
		int alea = min + (int)(Math.random() * ((max - min) + 1));
		return alea; 
	}
}
